package com.usdt.common.utils;

import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

import org.apache.commons.collections4.map.HashedMap;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;

import com.usdt.common.utils.spring.SpringUtils;

/**
 * 获取i18n资源文件
 * 
 * @author ruoyi
 */
public class MessageUtils
{
    /**
     * 根据消息键和参数 获取消息 委托给spring messageSource
     *
     * @param code 消息键
     * @param args 参数
     * @return 获取国际化翻译值
     */
    public static String message(String code, Object... args)
    {
    	try {
    		Locale locale = LocaleContextHolder.getLocale();
    		MessageSource messageSource = SpringUtils.getBean(MessageSource.class);
            return messageSource.getMessage(code, args, locale);
    	} catch (Exception e) {
    		return null;
    	}
        
    }
    
    /**
     * all messgae
     * @param code
     * @param args
     * @return
     */
    public static Map<String,String> allMessage()
    {
    	ResourceBundle bundle = ResourceBundle.getBundle("i18n/messages",LocaleContextHolder.getLocale());
    	
    	Map<String,String> map = new HashedMap<>();
    	for (String key : bundle.keySet()) {
			map.put(key, bundle.getString(key));
		}
    	return map;
    }
}
